﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using Public;

namespace Dal
{
    public class Link
    {
        public static DataTable MULTIPLE(string strCon)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;

            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Link_MULTIPLE");
            }
            catch
            {
                throw;
            }
            return dt;
        }

        public static DataTable Single(string strCon, string urlname)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@UrlName", SqlDbType.NVarChar, 1000);
            pars[0].Value = urlname;

            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Link_SINGLE_URL", pars);
            }
            catch
            {
                throw;
            }
            return dt;
        }

        public static DataTable Single(string strCon, int pkid)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@PKID", SqlDbType.Int);
            pars[0].Value = pkid;

            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Link_SINGLE_PKID", pars);
            }
            catch
            {
                throw;
            }
            return dt;
        }

        public static int Insert(string strCon,
                string txbLinkTitle,
                string txbLinkURL,
                int txbSortBy)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[3];
            pars[0] = new SqlParameter("@LinkTitle", SqlDbType.NVarChar, 20);
            pars[0].Value = txbLinkTitle;
            pars[1] = new SqlParameter("@LinkURL", SqlDbType.NVarChar);
            pars[1].Value = txbLinkURL;
            pars[2] = new SqlParameter("@SortBy", SqlDbType.TinyInt);
            pars[2].Value = txbSortBy;
            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Link_INSERT", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

        public static int Update(string strCon,
                string txbLinkTitle,
                string txbLinkURL,
                int txbSortBy,
                int txbPKID)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[4];
            pars[0] = new SqlParameter("@LinkTitle", SqlDbType.NVarChar, 20);
            pars[0].Value = txbLinkTitle;
            pars[1] = new SqlParameter("@LinkURL", SqlDbType.NVarChar);
            pars[1].Value = txbLinkURL;
            pars[2] = new SqlParameter("@SortBy", SqlDbType.TinyInt);
            pars[2].Value = txbSortBy;
            pars[3] = new SqlParameter("@PKID", SqlDbType.Int);
            pars[3].Value = txbPKID;

            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Link_UPDATE", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

        public static int Delete(string strCon, int pkid)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@PKID", SqlDbType.SmallInt);
            pars[0].Value = pkid;
            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Link_DELETE", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }
    }
}
